package com.polaris.live.utils.route

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.Parcelable
import com.blankj.utilcode.util.ActivityUtils
import com.polaris.live.App

/**
 * 转跳  Router  注意使用的时候一定要先添加path后在添加参数
 *
 * @author Created by 半仙 on 2023/9/4/004 14:49
 */
class Router(
    private var path: String = "",
) {

    private val intent = Intent()
    private var context: Context? = null

    fun setPath(path: String): Router {
        this.path = path
        return this
    }

    fun setActivity(activity: Activity?): Router {
        this.context = activity
        return this
    }

    fun setContext(context: Context?): Router {
        this.context = context
        return this
    }

    fun withBoolean(key: String, value: Boolean): Router {
        intent.putExtra(key, value)
        return this
    }

    fun withBooleanNullable(key: String, value: Boolean?): Router {
        if (value != null) {
            withBoolean(key, value)
        }
        return this
    }

    fun withByte(key: String, value: Byte): Router {
        intent.putExtra(key, value)
        return this
    }

    fun withByteNullable(key: String, value: Byte?): Router {
        if (value != null) {
            withByte(key, value)
        }
        return this
    }

    fun withShort(key: String, value: Short): Router {
        intent.putExtra(key, value)
        return this
    }

    fun withShortNullable(key: String, value: Short?): Router {
        if (value != null) {
            withShort(key, value)
        }
        return this
    }

    fun withInt(key: String, value: Int): Router {
        intent.putExtra(key, value)
        return this
    }

    fun withIntNullable(key: String, value: Int?): Router {
        if (value != null) {
            withInt(key, value)
        }
        return this
    }

    fun withLong(key: String, value: Long): Router {
        intent.putExtra(key, value)
        return this
    }

    fun withLongNullable(key: String, value: Long?): Router {
        if (value != null) {
            withLong(key, value)
        }
        return this
    }

    fun withFloat(key: String, value: Float): Router {
        intent.putExtra(key, value)
        return this
    }

    fun withFloatNullable(key: String, value: Float?): Router {
        if (value != null) {
            withFloat(key, value)
        }
        return this
    }

    fun withDouble(key: String, value: Double): Router {
        intent.putExtra(key, value)
        return this
    }

    fun withDoubleNullable(key: String, value: Double?): Router {
        if (value != null) {
            withDouble(key, value)
        }
        return this
    }

    fun withChar(key: String, value: Char): Router {
        intent.putExtra(key, value)
        return this
    }

    fun withCharNullable(key: String, value: Char?): Router {
        if (value != null) {
            withChar(key, value)
        }
        return this
    }

    fun withString(key: String, value: String): Router {
        intent.putExtra(key, value)
        return this
    }

    fun withStringNullable(key: String, value: String?): Router {
        if (value != null) {
            withString(key, value)
        }
        return this
    }

    fun withAny(key: String, value: Parcelable): Router {
        intent.putExtra(key, value)
        return this
    }

    fun <T : Parcelable> withArrayListAny(key: String, value: ArrayList<T>): Router {
        intent.putParcelableArrayListExtra(key, value)
        return this
    }

    fun withLongArray(key: String, value: LongArray): Router {
        intent.putExtra(key, value)
        return this
    }

    fun withAnyNullable(key: String, value: Parcelable?): Router {
        if (value != null) {
            withAny(key, value)
        }
        return this
    }

    /**
     * 跳转
     */
    fun navigation(context: Context?, options: Bundle? = null) {
        setContext(context)
        navigation(options)
    }

    /**
     * 跳转
     */
    fun navigationForResult(activity: Activity, requestCode: Int = -1) {
        if (requestCode != -1) {
            RouterManager.getImpl(path)?.let {
                intent.setClass(activity, it.clazz.java)
                when (it) {
                    is RouterAddressImpl -> {
                        //ignore
                    }

                    is RouterAddressExecImpl -> {
                        val toNavigation = it.block.run { invoke(activity, intent) }
                        if (!toNavigation) {
                            return
                        }
                    }
                }
                activity.startActivityForResult(intent, requestCode)
            }

        } else {
            setContext(activity)
            navigation()
        }
    }

    /**
     * 跳转
     */
    fun navigation(options: Bundle? = null) {
        val context = if (context != null) {
            context
        } else {
            ActivityUtils.getTopActivity()
        }
        val finalContext = if (context is Activity) {
            if (!context.isDestroyed && !context.isFinishing) {
                context
            } else {
                null
            }
        } else {
            context
        } ?: App.getAppContext()

        RouterManager.getImpl(path)?.let {
            intent.setClass(finalContext, it.clazz.java)
            when (it) {
                is RouterAddressImpl -> {
                    //ignore
                }

                is RouterAddressExecImpl -> {
                    val toNavigation = it.block.run { invoke(finalContext, intent) }
                    if (!toNavigation) {
                        return
                    }
                }
            }
            if (options != null) {
                ActivityUtils.startActivity(intent, options)
            } else {
                ActivityUtils.startActivity(intent)
            }

        }
    }

    companion object {

        val instance: Router
            get() = Router()

        /**
         * 构建一个路由
         */
        fun builder(path: String): Router {
            return Router(path)
        }
    }
}